home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / sviluppo / pike-0.4.0 / lib / include / process.pre.pike < prev    next >
Text File  |  1997-01-10  |  2KB  |  100 lines

  1. #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
  2. inherit "/precompiled/file";
  3.  
  4. varargs int exec(string file,string ... foo)
  5. {
  6.   string path;
  7.   if(search(file,"/"))
  8.     return exece(combine_path(getcwd(),file),foo,getenv());
  9.  
  10.   path=getenv("PATH");
  11.  
  12.   foreach(path/":",path)
  13.     if(file_stat(path=combine_path(path,file)))
  14.       return exece(path, foo,getenv());
  15.  
  16.   return 69;
  17. }
  18.  
  19. varargs int spawn(string s,object stdin,object stdout,object stderr)
  20. {
  21.   object p;
  22.   int pid;
  23.   string t;
  24.  
  25.   pid=fork();
  26.   
  27.   if(pid==-1)
  28.     error("No more processes.\n");
  29.  
  30.   if(pid)
  31.   {
  32.     return pid;
  33.   }else{
  34.     if(stdin)
  35.       stdin->dup2(File("stdin"));
  36.  
  37.     if(stdout)
  38.       stdout->dup2(File("stdout"));
  39.  
  40.     if(stderr)
  41.       stderr->dup2(File,"stderr");
  42.  
  43.     exec("/bin/sh","-c",s);
  44.     exit(69);
  45.   }
  46. }
  47.  
  48. string popen(string s)
  49. {
  50.   object p;
  51.   string t;
  52.  
  53.   p=file::pipe();
  54.   if(!p) error("Popen failed. (couldn't create pipe)\n");
  55.   spawn(s,0,p,0);
  56.   destruct(p);
  57.  
  58.   t=read(0x7fffffff);
  59.   if(!t)
  60.   {
  61.     int e;
  62.     e=errno();
  63.     close();
  64.     error("Popen failed with error "+e+".\n");
  65.   }else{
  66.     close();
  67.   }
  68.   return t;
  69. }
  70.  
  71. void system(string s)
  72. {
  73.   object p;
  74.   int pid;
  75.   string t;
  76.  
  77.   p=file::pipe();
  78.   if(!p) error("System() failed.\n");
  79.   p->set_close_on_exec(0);
  80.   if(pid=fork())
  81.   {
  82.     destruct(p);
  83.     /* Nothing will ever be written here, we are just waiting for it
  84.      * to close
  85.      */
  86.     file::read(1);
  87.   }else{
  88.     exec("/bin/sh","-c",s);
  89.     exit(69);
  90.   }
  91. }
  92.  
  93. void create()
  94. {
  95.   add_constant("system",system);
  96.   add_constant("exec",exec);
  97.   add_constant("spawn",spawn);
  98.   add_constant("popen",popen);
  99. }
  100.